home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / crlf / crlf.c next >
C/C++ Source or Header  |  1994-01-15  |  7KB  |  387 lines

  1. /* crlf.c 1.3 by entropy@terminator.rs.itd.umich.edu
  2.  
  3.    PUBLIC DOMAIN -- NO RIGHTS RESERVED
  4.    NO WARRANTY -- USE AT YOUR OWN RISK!!!!!!!!!
  5.    strips/adds carriage returns from text files
  6. */
  7. #ifndef __atarist__
  8. #ifdef __TOS__   /* #defined by TurboC / PureC */
  9.   #define __atarist__
  10. #endif
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <utime.h>
  19. #define UNUSED(x)
  20. #ifdef __atarist__
  21. #include <compiler.h>
  22. #ifdef __TURBOC__
  23. #include <sys\types.h>
  24. #include <sys\stat.h>
  25. #undef UNUSED
  26. #define UNUSED(x) ((void)(x))
  27. #else /* not __TURBOC__ */
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #endif /* not __TURBOC__ */
  31. #else /* not __atarist__ */
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #ifdef sun
  35. #include <utime.h>
  36. #endif /* sun */
  37. #ifndef EXIT_FAILURE
  38. #define EXIT_FAILURE 2
  39. #endif /* EXIT_FAILURE */
  40. #ifndef EXIT_SUCCESS
  41. #define EXIT_SUCCESS 0
  42. #endif /* EXIT_SUCCESS */
  43. #ifdef __STDC__
  44. #ifndef __NO___PROTO__
  45. #define __PROTO(x) x
  46. #endif /* not __NO___PROTO__ */
  47. #define __EXTERN
  48. #else /* not __STDC__ */
  49. #define __EXTERN extern
  50. #define __PROTO(x) ()
  51. #endif /* not __STDC__ */
  52. #endif /* not __atarist__ */
  53.  
  54. extern char *optarg;
  55. extern int optind, opterr;
  56.  
  57. #define CR_STRIP 0
  58. #define CR_ADD 1
  59. #define CR_NONE 2
  60.  
  61. char *program = NULL;
  62. int verbose = 0;
  63.  
  64. void perror2 __PROTO((char *msg1, char *msg2));
  65. int crlf __PROTO((char *filename, int mode));
  66. int simple_outchar __PROTO((FILE *ofp, int c));
  67. int tounx_outchar __PROTO((FILE *ofp, int c));
  68. int totos_outchar __PROTO((FILE *ofp, int c));
  69. int smart_copy __PROTO((char *ifn, char *ofn,
  70.                       int (*outcharfunc)(FILE *, int)));
  71.  
  72. void
  73. perror2(msg1, msg2)
  74.   char *msg1;
  75.   char *msg2;
  76. {
  77.   if (msg1 && *msg1)
  78.   {
  79.     fputs(msg1, stderr);
  80.     fputs(": ", stderr);
  81.   }
  82.   perror(msg2);
  83. }
  84.  
  85. int
  86. simple_outchar(ofp, c)
  87.   FILE *ofp;
  88.   int c;
  89. {
  90.   fputc(c, ofp);
  91.   return (c);
  92. }
  93.  
  94. int
  95. totos_outchar(ofp, c)
  96.   FILE *ofp;
  97.   int c;
  98. {
  99.   if (c == '\r' ) /* first strip all CRs... */
  100.     return c;
  101.   if (c == '\n')
  102.   {
  103.     fputc('\r', ofp);
  104.   }
  105.   fputc(c, ofp);
  106.   return (c);
  107. }
  108.  
  109. int
  110. tounx_outchar(ofp, c)
  111.   FILE *ofp;
  112.   int c;
  113. {
  114.   if (c != '\r')
  115.   {
  116.     fputc(c, ofp);
  117.   }
  118.   return (c);
  119. }
  120.  
  121. int
  122. smart_copy(ifn, ofn, outcharfunc)
  123.   char *ifn;
  124.   char *ofn;
  125.   int (*outcharfunc) __PROTO((FILE *, int));
  126. {
  127.   FILE *ifp;
  128.   FILE *ofp;
  129.   int c;
  130.  
  131.   if (*ifn) 
  132.   {
  133.     if ((ifp = fopen(ifn, "rb")) == NULL)
  134.     {
  135.       perror2(program, ifn);
  136.       return (-1);
  137.     }
  138.   }
  139.   else
  140.   {
  141.     ifp = stdin;
  142.   }
  143.   if (*ofn) 
  144.   {
  145.     if ((ofp = fopen(ofn, "wb")) == NULL)
  146.     {
  147.       fclose(ifp);
  148.       perror2(program, ofn);
  149.       return (-1);
  150.     }
  151.   }
  152.   else
  153.   {
  154.     ofp = stdout;
  155.   }
  156.   setvbuf(ofp, NULL, _IOFBF, 50*1024L);
  157.   setvbuf(ifp, NULL, _IOFBF, 50*1024L);
  158.   c = fgetc(ifp);
  159.   while (!(feof(ifp)) && !(ferror(ifp)) && !(ferror(ofp)))
  160.   {
  161.     (*outcharfunc)(ofp, c);
  162.     c = fgetc(ifp);
  163.   }
  164.   if (ferror(ifp))
  165.   {
  166.     perror2(program, ifn);
  167.     if (*ifn)
  168.     {
  169.       fclose(ifp);
  170.     }
  171.     if (*ofn)
  172.     {
  173.       fclose(ofp);
  174.     }
  175.     return (-1);
  176.   }
  177.   if (ferror(ofp))
  178.   {
  179.     perror2(program, ofn);
  180.     if (*ifn)
  181.     {
  182.       fclose(ifp);
  183.     }
  184.     if (*ofn)
  185.     {
  186.       fclose(ofp);
  187.     }
  188.     return (-1);
  189.   }
  190.   if (*ifn && fclose(ifp) == EOF)
  191.   {
  192.     perror2(program, ifn);
  193.     if (*ofn)
  194.     {
  195.       fclose(ofp);
  196.     }
  197.     return (-1);
  198.   }
  199.   if (*ofn && fclose(ofp) == EOF)
  200.   {
  201.     perror2(program, ofn);
  202.     return (-1);
  203.   }
  204.   return (0);
  205. }
  206.  
  207. int
  208. crlf(filename, mode)
  209.   char *filename;
  210.   int mode; /* 0 == strip CR's, 1 == add CR's */
  211. {
  212.   char tempname[FILENAME_MAX];
  213.   char *ev;
  214.   int (*outcharfunc) __PROTO((FILE *, int));
  215.  
  216.   switch (mode)
  217.   {
  218.     case CR_ADD:
  219.       outcharfunc = totos_outchar;
  220.       break;
  221.     case CR_STRIP:
  222.     default:
  223.       outcharfunc = tounx_outchar;
  224.       break;
  225.   }
  226.   if (verbose)
  227.   {
  228.     fputs(filename, stderr);
  229.     fputc('\n', stderr);
  230.   }
  231.   if (*filename)
  232.   {
  233.     *tempname = '\0';
  234.     if ((ev = getenv("TEMP")) || (ev = getenv("TMP"))
  235.         || (ev = getenv("TMPDIR")))
  236.     {
  237.       strcpy(tempname, ev);
  238.       strcat(tempname, "/");
  239.     }
  240.     strcat(tempname, "crlfXXXX");
  241.     if (mktemp(tempname) == NULL)
  242.     {
  243.       fputs(program, stderr);
  244.       fputs(": could not get a temporary filename\n", stderr);
  245.       return (-1);
  246.     }
  247.     if (smart_copy(filename, tempname, outcharfunc))
  248.     {
  249.       return (-1);
  250.     }
  251.     if (rename(tempname, filename))
  252.     {
  253.       if (smart_copy(tempname, filename, simple_outchar))
  254.       {
  255.         return (-1);
  256.       }
  257.       if (unlink(tempname))
  258.       {
  259.         perror2(program, tempname);
  260.         return (-1);
  261.       }
  262.     }
  263.   }
  264.   else
  265.   {
  266.     if (smart_copy("", "", outcharfunc))
  267.     {
  268.       return (-1);
  269.     }
  270.   }
  271.   return (0);
  272. }
  273.  
  274. int
  275. main(int argc, char *argv[], char *envp[])
  276. {
  277.   char *fn = NULL;
  278.   int mode = CR_NONE;
  279.   int c;
  280.   int err = 0;
  281.   struct stat st;
  282.   struct utimbuf ut;
  283.  
  284.   UNUSED(envp);
  285.   program = argv[0];
  286.   while ((c = getopt(argc, argv, "asv")) != EOF)
  287.   {
  288.     switch (c)
  289.     {
  290.       case 'a':
  291.         if (mode == CR_NONE)
  292.         {
  293.           mode = CR_ADD;
  294.         }
  295.         else
  296.         {
  297.           err++;
  298.         }
  299.         break;
  300.       case 's':
  301.         if (mode == CR_NONE)
  302.         {
  303.           mode = CR_STRIP;
  304.         }
  305.         else
  306.         {
  307.           err++;
  308.         }
  309.         break;
  310.       case 'v':
  311.         verbose = 1;
  312.         break;
  313.       case '?':
  314.       default:
  315.         err++;
  316.         break;
  317.     }
  318.   }
  319.   if (mode == CR_NONE)
  320.   {
  321.     err++;
  322.   }
  323.   if (err)
  324.   {
  325.     fputs("usage:  ", stderr);
  326.     fputs(program, stderr);
  327.     fputs(" -s[v] [file [file2 [...]]] (to strip carriage returns)\n", stderr);
  328.     fputs("        ", stderr);
  329.     fputs(program, stderr);
  330.     fputs(" -a[v] [file [file2 [...]]] (to add carriage returns)\n", stderr);
  331.     exit(EXIT_FAILURE);
  332.   }
  333.   if (optind == argc)
  334.   {
  335.     if (crlf("", mode) != 0)
  336.     {
  337.       err++;
  338.     }
  339.   }
  340.   else
  341.   {
  342.     for ( ; optind < argc; optind++)
  343.     {
  344.       fn = argv[optind];
  345.       if (access(fn, 4))
  346.       {
  347.         err++;
  348.         perror2(program, fn);
  349.         continue;
  350.       }
  351.       if (stat(fn, &st))
  352.       {
  353.         err++;
  354.         perror2(program, fn);
  355.       }
  356.       if (!(S_ISREG(st.st_mode)))
  357.       {
  358.         if (verbose)
  359.         {
  360.           fputs("crlf:  ignoring non-regular file ", stderr);
  361.           fputs(fn, stderr);
  362.           fputc('\n', stdout);
  363.         }
  364.         continue;
  365.       }
  366.       ut.actime = st.st_atime;
  367.       ut.modtime = st.st_mtime;
  368.       if (crlf(fn, mode) != 0)
  369.       {
  370.         err++;
  371.         continue;
  372.       }
  373.       if (utime(fn, &ut))
  374.       {
  375.         err++;
  376.         perror2(program, fn);
  377.       }
  378.     }
  379.   }
  380.   if (err)
  381.   {
  382.     exit(EXIT_FAILURE);
  383.   }
  384.   exit(EXIT_SUCCESS);
  385.   return (0);
  386. }
  387.